package com.Akronmcp.hta.obj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import com.Akronmcp.hta.main.HSender;
import com.comphenix.protocol.events.PacketContainer;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import me.clip.placeholderapi.PlaceholderAPI;

/**hopchat类  tellraw文本*/
public class HopChat extends HPar{
	

   public JsonArray ja = new JsonArray();
   JsonObject jo;
   
   boolean iscreated = false;
   //是否已创建为json
   
   String caction;
   String cvalue;
   //点击事件
   
   String haction;
   String hvalue;
   //悬停事件
   
   String prefix = "";
   String suffix = "";
   //前后缀
   PacketContainer packet;
   
   public HopChat(HManager hta)
   {
      super(hta);
   }
   
   /**获取文本*/
   public String getText()
   {
	   return prefix + text + suffix;
   }
   
   /**设置点击事件*/
   public void setClickEvent(String action,String value)
   {
	   caction = action.toLowerCase();
	   cvalue = value.toLowerCase();
   }
   
   /**设置悬停事件*/
   public void setHoverEvent(String action,String value)
   {
	   haction = action.toLowerCase();
	   hvalue = value.toLowerCase();
   }
   
   /**清空json*/
   public void clear()
   {
	   for(int i=0;i<ja.size();i++)
	   {
		   this.ja.remove(i);
	   }
   }
   
   /**从引擎列表中移除*/
   public void removeFromList()
   {
	   HManager.hchatMap.remove(this.id);
   }
   
   /**直接设置json*/
   public void setJson(JsonArray ja)
   {
	   this.clear();
	   this.ja.add(ja);
   }
   
   public void addJson(JsonArray ja)
   {
	   this.ja.add(ja);
   }
   
   /**从string生成json*/
   public void setFromJsonString(String json)
   {
	    Gson gson = new Gson();
	    setJson(gson.fromJson(json, JsonArray.class));
   }
   
   /**创建json*/
   public void create()
   {
	   hta.createToJson(this);
	   if(!papi)
	   {
		   String json = toJsonString();
		   packet = HSender.getPacket(json!=null?json:"[\"empty\"]");
	   }
   }
   
   /**加入到引擎列表*/
   public void addToList()
   {
	   if(id!=null)
	   hta.addText(this);
   }
   /**加入到引擎列表*/
   public void addToList(String id) 
   {
	 this.id = id;
	 addToList();
   }
   
   
   /**发送给玩家,参数2为papi所解析玩家*/
   public void send(List<Player> pl)
   {    
	   if(pl==null||pl.isEmpty())
	   return;
	   
	   String tjson = "";
   
	   for(Player p:pl)
	   {  //遍历发送
		  Player pp = p;
	      
		  if(papi)
		  {//解析papi
			  tjson = PlaceholderAPI.setPlaceholders(pp, toJsonString());
		      HSender.sendTellraw(p,HSender.getPacket(tjson));
		  }else
		  {
		      HSender.sendTellraw(p,packet);
		  }
	   }
	   
	   sendSubtext(pl);  	
	   //发送伴随文本
   }
   
   
   /**发送给所有玩家*/
   public void sendToAll() 
    {
	   List<Player> pl = new ArrayList<Player>(Bukkit.getOnlinePlayers());
		if(pl.isEmpty())
		return;	 
	
		send(pl);
    }
	
   
   public String getCaction()
   {
	   return this.caction;
   }  
   
   public String getCvalue()
   {
	   return this.cvalue;
   }  
   
   public String getHaction()
   {
	   return this.haction;
   }  
      
   public String getHvalue()
   {
	   return this.hvalue;
   }  
      
   
   public JsonArray getJson()
   {
	   return this.ja;
   }  
   
   public void setPrefix(String str)
   {
	   prefix = str;
   }
   
   public void setSuffix(String str)
   {
	   suffix = str;
   }

   public String getPrefix()
   {
	   return prefix;
   }
   public String getSuffix()
   {
	   return suffix;
   }

   
   String cachejson = null;
   public String toJsonString()
   {
	   if(cachejson==null)
		 cachejson=ja.toString();
	   
	   return cachejson;
   }
   
   public String toString()
   {
	   return text; 
   }
	
   
   public boolean isCreated()
   {
	   return this.iscreated;
   }

   /**直接通过文本生成临时hopchat发送给玩家*/
   public static void sendBy(List<Player> pl,String text,boolean ispapi)
	{
	   HopChat htx = new HopChat(new HManager());
	   htx.setPapi(ispapi);
       htx.setText(text);
       htx.create();
       
       for(Player p:pl)
       htx.send(p);
	}
  
   /**直接通过文本生成临时hopchat发送给玩家*/
   public static void sendtoallBy(String text,boolean ispapi)
   {
	  sendBy(new ArrayList<Player>(Bukkit.getOnlinePlayers()) ,text,ispapi);
   }

   /**直接通过文本生成临时hopchat发送给玩家*/
   public static void sendBy(Player p,String text,boolean ispapi)
   {
	   sendBy(Arrays.asList(p),text,ispapi);
	   
   }








    
	
}
